package oa.dao.impl;

import java.util.List;

import oa.base.BaseDao;
import oa.dao.MessageDao;
import oa.entity.Message;
import oa.exception.DaoException;

public class MessageDaoImpl extends BaseDao<Message> implements MessageDao {
	public List<Message> selectMessages(int companyid, Integer pageNo, Integer row) throws DaoException {
		if(pageNo == 0 && row == 0) {
			return executeQuery("SELECT * FROM message");
		}else if(companyid!=0){
			return executeQuery("SELECT * FROM message  WHERE  company_id = ? LIMIT ?,?",companyid,(pageNo-1)*row,row);
		}else{
			return executeQuery("SELECT * FROM message  LIMIT ?,?",(pageNo-1)*row,row);
		}
	
	}


	@Override
	public List<Message> selectMessage(int id)  throws DaoException{
		return executeQuery("SELECT * FROM message WHERE message_id = ?",id);
	}

	@Override
	public boolean deleteMessage(int id) throws DaoException {
		// TODO Auto-generated method stub
		return executeUpdate("DELETE  FROM message WHERE message_id=?",id)>0;
	}

	@Override
	public boolean updateMessage(Message message) throws DaoException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addMessage(Message message) throws DaoException {
		return executeUpdate("INSERT INTO `oa`.`message` ( `message_content`, `message_times`, `message_title`, `message_types`, `company_id`) VALUES ( ?, ?, ?, ?, ?)",
				message.getMessageContent(),message.getMessageTimes(),message.getMessageTitle(),message.getMessageTypes(),message.getCompanyId())>0;
	}


	@Override
	public int countMessage(int MessagecompId) throws DaoException {
		if(MessagecompId!=0) {
			return executeIntQuery("SELECT COUNT(*) FROM message WHERE company_id =?",MessagecompId);
		}else {
			return executeIntQuery("SELECT COUNT(*) FROM message ");
		}
		
	}


	

}
